package com.koron.css2.newProjectCenter.print;

import com.koron.css2.ServerInterface;
import com.koron.css2.newProjectCenter.bean.PjInfoBean;
import com.koron.css2.newProjectCenter.bean.query.PjInfoQueryBean;
import com.koron.css2.newProjectCenter.mapper.PjCheckInfoMapper;
import com.koron.css2.newProjectCenter.mapper.PjInfoMapper;
import com.koron.css2.newProjectCenter.vo.PjCheckInfoVO;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.css2.systemManage.bean.OrgUserBean;
import com.koron.css2.systemManage.mapper.OrgUserMapper;
import com.koron.util.Constant;
import com.koron.util.PrintInterfaceImpl;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PjCheckInfoPrint implements ServerInterface {
    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            Map<String, String> param = JsonUtils.objectToPojo(req.getData(), Map.class);
            String billNo = param.get("billNo");
            String departBelong = param.get("departBelong");
            ArrayList<HashMap<String, String>> result = new ArrayList<>();

            PjCheckInfoMapper pjCheckInfoMapper = factory.getMapper(PjCheckInfoMapper.class);
            PjInfoMapper pjInfoMapper = factory.getMapper(PjInfoMapper.class);
            OrgUserMapper orgUserMapper = factory.getMapper(OrgUserMapper.class);

            // 1. 断当前单号的当前部门的验收项是否存在；
            int iCount = pjCheckInfoMapper.countByBillNoAndDepartBelong(billNo, departBelong);
            if (iCount == 0) {
                return MessageBean.create(Constant.MESSAGE_INT_FAIL, "当前工程的当前部门未进行验收。", null);
            }
            // 2. 查询当前用户的最新的工程验收信息
            // 2.1 查询最大版本号
            int version = pjCheckInfoMapper.maxVersion(billNo, departBelong, userInfo.getUserInfo().getAcount());
            // 2.2 查询数据
            List<PjCheckInfoVO> pjCheckInfoVOList = pjCheckInfoMapper.selectByBillNoAndVersionAndDepartBelong(billNo, version, departBelong);
            // 3. 查询工程信息
            PjInfoQueryBean pjInfoQueryBean = new PjInfoQueryBean();
            pjInfoQueryBean.setBillNo(billNo);
            PjInfoBean pjInfoBean = pjInfoMapper.getSingle(pjInfoQueryBean);
            // 4. 循环设置数据
            pjCheckInfoVOList.forEach(pjCheckInfoVO -> {
                HashMap<String, String> map = new HashMap<>();
                // 4.1 pjName
                map.put("pjName", pjInfoBean.getPjName());
                // 4.2 billNo
                map.put("billNo", billNo);
                // 4.3 pjTypeName
                String pjType = pjInfoBean.getPjType();
                if (StringUtils.isNotEmpty(pjType)) {
                    map.put("pjTypeName", Tools.getDicNameByCodeAndValue(factory, "PPT", pjType));
                }
                // 4.4 constructionUnit
                map.put("constructionUnit", pjInfoBean.getConstructionUnit());
                // 4.5 departBelong
                map.put("departBelong", pjCheckInfoVO.getDepartBelong());
                // 4.6 checkItem
                map.put("checkItem", pjCheckInfoVO.getCheckItem());
                // 4.7 checkSubItem
                map.put("checkSubItem", pjCheckInfoVO.getCheckSubitem());
                // 4.8 checkStaffName
                String checkStaff = pjCheckInfoVO.getCheckStaff();
                OrgUserBean orgUserBean = orgUserMapper.selectByAccount(checkStaff);
                if (orgUserBean != null) {
                    map.put("checkStaffName", orgUserBean.getUserName());
                }
                // 4.9 passFlagName
                String passFlag = pjCheckInfoVO.getPassFlag();
                if (StringUtils.isNotEmpty(passFlag) && passFlag.equals("0")) {
                    map.put("passFlagName", "未通过");
                }
                if (StringUtils.isNotEmpty(passFlag) && passFlag.equals("1")) {
                    map.put("passFlagName", "通过");
                }
                if (StringUtils.isNotEmpty(passFlag) && passFlag.equals("2")) {
                    map.put("passFlagName", "保留通过");
                }
                // 4.10 comments
                map.put("comments", pjCheckInfoVO.getComments());
                result.add(map);
            });
            // 5. 生成打印id
            String key = PrintInterfaceImpl.setPrintData(result);
            List<String> keys = new ArrayList<>();
            keys.add(key);
            // 6. 返回打印id
            MessageBean<List> messageBean = MessageBean.create(Constant.MESSAGE_INT_SUCCESS,"当前用户最近一次工程验收打印成功",List.class);
            messageBean.setData(keys);
            return messageBean;
        } catch (Exception e) {
            logger.error("当前用户最近一次工程验收打印失败",e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL,"当前用户最近一次工程验收打印失败",null);
        }
    }
}
